//
//  CTVIntroPlayerController.m
//  Channltv
//
//  Created by Bruno Keymolen on 13/11/07.
//  Copyright 2007 Bruno Keymolen. All rights reserved.
//

#import "CTVIntroPlayerController.h"
#import "CTVCache.h"
#import "CTVHTTPMediaDownload.h"
#import "CTVIntroOverlayController.h"

@implementation CTVIntroPlayerController


-(id)initWithScene:(BRRenderScene*)scene introduceClip:(CTVClip*)clip forChannel:(CTVChannel*)channel{
	self = [super initWithScene:scene];
	if(self == nil){
		return nil;
	}
	
	m_playClipNow = YES;
	
	m_clip = clip;
	[m_clip retain];
	
	m_channel = channel;
	[m_channel retain];

	NSString* iconPath =  [[NSBundle bundleForClass: [self class]] pathForResource:@"logo" ofType: @"png"];
	
	m_introClip = [[CTVClip alloc] init];
	[m_introClip setTitle:@"Intro"];
	[m_introClip setUrl:@"http://www.channl.tv/download/getlink?clip_id=1265"];
	[m_introClip setThumbUrl:iconPath];
	
	
	return self;
}

-(void)dealloc{
	[m_clip release];
	[m_channel release];
	[m_introClip release];
	
	if(m_downloadController == nil){
		[m_downloadController release];
	}
	if(m_videoPlayerController != nil){
		[m_videoPlayerController release];
	}
	[super dealloc];
}


-(void)play{
	NSString* cacheEntry = [CTVCache getMediaCacheEntryForURL:[m_introClip getUrl]];		
	
	if(cacheEntry == nil){
		//clip is not in cache, ask cahche to start download or to return the current downloader in case of a download in progress
		NSString* cachePath;
		CTVHTTPMediaDownload* downloader = [[CTVCache GetInstance] cacheURLString:[m_introClip getUrl] cachePathNoExt:&cachePath andClip:m_clip];
		
		
		CGImageRef img = (CGImageRef)CreateImageForURL( [m_introClip getThumbUrl] );

		if(m_downloadController != nil){
			[m_downloadController release];
		}

		m_downloadController = [[CTVMediaDownloadController alloc] initWithScene:[self scene] andClip:m_introClip andImage:img andMediaDownload:downloader andTitle:[m_introClip getTitle] forChannel:m_channel ];
		[[self stack] pushController:m_downloadController];

		return;
	}
	
NSLog(@"Play intro: %@", [m_introClip getUrl]);
	
	NSURL* mediaURL = [NSURL fileURLWithPath:cacheEntry];
	
	//Play Intro
	BRSimpleMediaAsset *asset = [[[BRSimpleMediaAsset alloc] initWithMediaURL:mediaURL ] autorelease];
	
	//Player, set media asset
	id player = [[[BRQTKitVideoPlayer alloc] init] autorelease];
	NSError *error = [[NSError alloc] init];
	[player setMedia:asset error:&error];

	//to the screen; controller, set player
	if(m_videoPlayerController != nil){
		[m_videoPlayerController release];
	}
	m_videoPlayerController = [[CTVVideoPlayerController alloc] initWithScene:[self scene]];
	[m_videoPlayerController setVideoPlayer:player];
		  
	[error release];

	[[self stack] pushController:m_videoPlayerController];
	
	
	//Show overlay
//	CTVIntroOverlayController* overlay = [[[CTVIntroOverlayController alloc] initWithScene:[self scene] andTitle:[m_clip getTitle]] autorelease];
//	[[self stack] pushController:overlay];

	
}

-(BOOL)playClipNow{
	return m_playClipNow;
}


//BRLayerController
- (void)wasExhumedByPoppingController:(id)controller{
	[super wasExhumedByPoppingController:controller];	

	if(controller == m_videoPlayerController){
		//We are done playing the intro
		[[self stack] removeController:self];	
	}else if(controller == m_downloadController){
		//Play it now
		[self play];
	}
	
}


-(BOOL)brEventAction:(BREvent*)event{
	BOOL ret;
	ret = [super brEventAction:event];

	//We are only interested in the key-down events.
	if([event value] == 0){
		return [super brEventAction:event];
	}
	
	
	if([event remoteAction] == kBRRemoteActionMenu){
		m_playClipNow = NO;
	}

	return ret;
}


@end
